package be.djdb.generators;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JOptionPane;



import be.djdb.generators.data.SuperItems;
import be.djdb.mdi.MYMDI;
import be.djdb.mdi.Mywindowmenu;
import be.djdb.utils.Projectresources;
import be.djdb.utils.Projectsettings;
/**
* @author Lieven Roegiers
* @copyright 2007
* @from JAVA_mylibs
*/
public class MYFORM extends JFrame{
	protected Projectsettings settings;
	private static final long serialVersionUID = -3763471676897721840L;
	private SuperItems translatdata;
	private boolean closemsg = false;
	private static String closemesage;
	private String settingname ="Projectsettings";
	int usedheight=0;
	int usedwitdh=0;
	int height=0;
	int witdh=0;
	protected MYMDI body= new MYMDI();
	 static ComponentListener mdiresize = new ComponentAdapter(){
	      public void componentShown(ComponentEvent evt){
	        Component c = evt.getComponent();
	        System.out.println("Component is now visible");
	      }
	      public void componentHidden(ComponentEvent evt){
	        Component c = (Component) evt.getSource();
	        System.out.println("Component is now hidden");
	      }
	      public void componentResized(ComponentEvent evt){
	        Component c = evt.getComponent();
	        ((MYFORM)c).setgui(c.getLocation(),c.getSize());
	      }
	    };
	    public void setgui(Point location,Dimension size){
	    	//System.out.println("ook ergen anders");
	        //System.out.println("Get x:"+location.x);
	        //System.out.println("Get y:"+location.y);
	        resizeto(size.width,size.height);
	    }
	/**
	 * 
	 */
	private void initComponents() {
	        pack();
	}
	/**
	 * 
	 */
	public MYFORM() {
			this("NULL");
			addComponentListener(mdiresize);
	}
    /**
     * @param title
     */
    public MYFORM(String title) {
		this(title,null,title,false);
    }
    public MYFORM(String title,String resourcebunlename,boolean closemsg){
    		this(title,resourcebunlename,title,closemsg);
    }
    /**
     * @param title
     * @param resourcebunlename
     * @param settingname
     * @param closemsg
     */
    public MYFORM(String title,String resourcebunlename,String settingname,boolean closemsg){
    	super(title);
    	this.settingname = settingname;
		translatdata = new SuperItems(resourcebunlename);
		this.closemesage = translatdata.getlblFor("closemsg");
		addComponentListener(mdiresize);
		settings = new Projectsettings(settingname);
		settings.LoadSettings();
		setLayout(new BorderLayout());
		body.setSize(500, 500);
		setSize(500, 500);
		super.add(body,BorderLayout.CENTER);
	    if(closemsg){
	    	setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
	    	addWindowListener(Close_WindowAdapter);
	    }else{
	    	setDefaultCloseOperation(EXIT_ON_CLOSE);
	    } 
	}
	/**
	 * 
	 * @param comp
	 * @param border
	 * @param dim
	 */
	public void add(Component comp, BorderLayout border,Dimension dim) {
		super.add(comp, border);
		if (border.equals(BorderLayout.SOUTH) ||border.equals(BorderLayout.NORTH)){
			this.usedheight += dim.height; 				
		}
		if (border.equals(BorderLayout.WEST) ||border.equals(BorderLayout.EAST)){
			this.usedwitdh += dim.width; 				
		}
    }
	public JMenu getwindowmenu(){
		return new Mywindowmenu(body,translatdata);
	}
/**
 * 
 * @param frame
 */
	public void add(JInternalFrame frame) {
		body.add(frame);
    }
	
	@Deprecated
    public void add(Component comp, Object constraints, int index) {
		super.add(comp, constraints,index);
    }
	/**
	 * 
	 */
	static WindowAdapter Close_WindowAdapter = new WindowAdapter() {
		public void windowClosing(WindowEvent e){ int answer = JOptionPane.showConfirmDialog(null,closemesage , "CV Lieven sluiten", JOptionPane.YES_NO_OPTION);
		   if (answer == JOptionPane.YES_OPTION) {
			   System.exit(0);
		   }else if (answer == JOptionPane.NO_OPTION){  return;
		   }
		}
	};//de puntcoma voor afsluiten windowadapter
    /**
     * @param width
     * @param height
     */
    public void resizeto(int width , int height){
    	Dimension screenSize = this.getscreensize();
        Dimension frameSize = getSize();
        if(height>screenSize.height||width>screenSize.width){
            frameSize.height = height;
            frameSize.width = width;
        }
        setLocation((screenSize.width-frameSize.width)/2,(screenSize.height-frameSize.height)/2);
    } 
/**
 * 
 * @return
 */
    public Dimension getscreensize(){
       return Toolkit.getDefaultToolkit().getScreenSize();
    }
    public void setsetting(){
    	settings.SaveSettings();
    }
    @Override
    protected void finalize() throws Throwable {
    	settings.SaveSettings();
    	super.finalize();
    }
}
